from aiogram.filters import BaseFilter
from aiogram import types

from database.models import TgUser


class IsRegistered(BaseFilter):
    """
    Зарегистрирован ли пользователь в боте.
    """
    def __init__(self, is_registered: bool = True):
        self.is_registered = is_registered

    async def __call__(self, update: types.Message | types.CallbackQuery) -> bool:
        user: TgUser | None = TgUser.objects.filter(user_id=update.from_user.id).first()
        if user:
            return True is self.is_registered
        return False is self.is_registered


class IsActive(BaseFilter):
    """Не в бане ли пользователь"""
    def __init__(self, is_active: bool = True):
        self.is_active = is_active

    async def __call__(self, update: types.Message | types.CallbackQuery) -> bool:
        user: TgUser | None = TgUser.objects.filter(user_id=update.from_user.id).first()
        if user and user.status != TgUser.BANNED:
            return True is self.is_active
        return False is self.is_active


class IsAdmin(BaseFilter):
    """
    Является ли пользователь администратором бота.
    """
    def __init__(self, is_admin: bool = True):
        self.is_admin = is_admin

    async def __call__(self, update: types.Message | types.CallbackQuery) -> bool:
        user = TgUser.objects.filter(user_id=update.from_user.id).first()
        if user and user.is_admin():
            return True is self.is_admin
        return False is self.is_admin
